<?php
/**
 * 司机提现申请
 */

namespace App\Services;


use App\Models\DriverRecordLog;
use App\Models\DriverWallet;
use App\Models\DriverWallets;
use App\Models\DriverWithdraw;
use App\Models\User;
use Illuminate\Support\Facades\DB;

class WithdrawService extends BaseService
{
    /**
     * 司机提现申请
     */
    public function withdraw_add(array $data, string $type, User $user)
    {

        $drivers_id = $user->driver()->first()->id;
        if (empty($drivers_id)) return_bobc('司机不存在');
        $amount = DriverWallets::query()->where('driver_id', $drivers_id)->first('amount');
        if (empty($amount)) return_bobc('暂时没有可以提现的金额哟');
        if ($amount->amount < $data['money']) return_bobc('余额不足请重新填写提现金额');
        $company_id = $user->driver()->first()->company_id;
        return $this->withdraw_save($drivers_id, $company_id, $data['money'], $data['wallet_id'], $type);

    }

    /**
     * @param int $drivers_id
     * @param int $company_id
     * @param float $money
     * @param int $wallet_id
     * 提现数据插入
     */
    protected function withdraw_save(int $drivers_id, int $company_id, float $money, int $wallet_id, string $type)
    {
        $withdraw = new  DriverWithdraw();
        try {
            DB::transaction(function () use ($withdraw, &$drivers_id, &$company_id, &$money, $wallet_id, $type) {
                $withdraw->driver_id = $drivers_id;
                $withdraw->company_id = $company_id;
                $withdraw->money = $money;
                $withdraw->wallet_id = $wallet_id;
                $withdraw->status = 1;
                $withdraw->in_type = '0';  // 0 减 1加
                $withdraw->withdraw_sn = createOrderNo();
                $withdraw->type = $type;
                $res = $withdraw->save();
                if ($res) {
                    self::driver_record_log($drivers_id, '余额提现', $money);
                    DriverWallets::query()->where('driver_id', $drivers_id)->where('type', $type)->update(
                        [
                            'amount' => DB::raw('amount - ' . $money),
                            'balance' => DB::raw('balance - ' . $money),
                        ]); //扣除司机账户可提现金额/和司机账户余额
                }

            });
        } catch (\Exception $e) {
            return $e->getMessage();
        }
        return true;
    }

    /**
     * 记录提现日志
     */
    private function driver_record_log(int $drivers_id, $name, $money)
    {
        $driver_record_log = new DriverRecordLog();
        $driver_record_log->driver_id = $drivers_id;
        $driver_record_log->name = $name;
        $driver_record_log->money = $money;
        $driver_record_log->stard_time = date('Y-m-d H:i:s');
        $driver_record_log->order_sn = $drivers_id;
        $driver_record_log->status = '0';
        $driver_record_log->set_type = '1';
        $driver_record_log->note = '余额提现';
        $driver_record_log->save();
    }

    /**
     * 司机账号列表
     */
    public function driver_wallet_list(User $user)
    {
        $drivers_id = $user->driver()->first() ? $user->driver()->first()->id : 0;
        return DriverWallet::query()->where('driver_id', $drivers_id)->get();
    }

    /**
     * 新增提现账户
     */
    public function driver_wallet_insert($account, $type, $bank_name, $account_name, $phone, User $user)
    {
        $drivers_id = $user->driver()->first() ? $user->driver()->first()->id : 0;
        if (!$drivers_id) return_bob('司机不存在');
        return $this->wallet_add($drivers_id, $type, $account, $account_name, $bank_name, $phone);
    }

    /**
     * @param int $driver_id
     * @param int $type
     * @param string $account
     * @return bool
     * 添加账号
     */
    protected function wallet_add(int $driver_id, int $type, string $account, string $bank_name, string $account_name, string $phone)
    {
        $flight = new  DriverWallet();
        $flight->driver_id = $driver_id ? $driver_id : '';
        $flight->type = $type ? $type : '';
        $flight->account = $account ? $account : '';
        $flight->account_name = $account_name ? $account_name : '';
        $flight->bank_name = $bank_name ? $bank_name : '';
        $flight->phone = $phone ? $phone : '';
        if (!$flight->save()) {
            return_bobc('添加失败');
        }
        return $flight->where('id', $flight->id)->first();
    }

    /**
     * 删除提现账号
     */
    public function driver_wallet_del(int $id)
    {
        $model = new DriverWallet();
        if ($model::destroy($id)) {
            return true;
        }
        return false;
    }

    /**
     * 提现详情
     */
    public function driver_withdraw_show(int $id)
    {
        return DriverWithdraw::query()->where('id', $id)->first()->toArray();
    }

    /**
     * 上一次提现几率查询
     */
    public function driver_wallet_status(User $user)
    {
        $drivers_id = $user->driver()->first() ? $user->driver()->first()->id : 0;
        if (!$drivers_id) return_bobc('司机不存在');
        $info = DriverWithdraw::query()->where('driver_id', $drivers_id)->orderByDesc('id')->first('wallet_id');
        if ($info) {
            return DriverWallet::query()->where('id', $info->wallet_id)->first();
        }
        return null;
    }
}
